package com.telecomyt.jwportal.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.telecomyt.jwportal.MainActivity;
import com.telecomyt.jwportal.activity.camera.CameraActivity;
import com.telecomyt.jwportal.utils.ToastUtils;
import com.telecomyt.jwportal.widget.VoiceFloatingView;


public class CameraService extends android.app.Service {
    public static CameraService service;
    public static boolean isStart;
    private VoiceFloatingView mVoiceFloatingView;
    public static final String ACTION_SHOW_FLOATING = "action_show_floating";
    public static final String ACTION_DISMISS_FLOATING = "action_dismiss_floating";

    public class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            VoiceFloatingView voiceFloatingView;
            if ("action_show_floating".equals(intent != null ? intent.getAction() : null)) {
                voiceFloatingView = CameraService.this.mVoiceFloatingView;
                if (voiceFloatingView != null) {
                    voiceFloatingView.show();
                }
            } else if ("action_dismiss_floating".equals(intent != null ? intent.getAction() : null)) {
                voiceFloatingView = CameraService.this.mVoiceFloatingView;
                if (voiceFloatingView != null) {
                    voiceFloatingView.dismiss();
                }
            }
        }
    }


    public void onCreate() {
        super.onCreate();
        service = this;
        isStart = true;
        this.mVoiceFloatingView = new VoiceFloatingView((Context) this);
        IntentFilter intentFilter = new IntentFilter("action_show_floating");
        intentFilter.addAction("action_dismiss_floating");
        LocalBroadcastManager
                .getInstance(this)
                .registerReceiver(new MyBroadcastReceiver(), intentFilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mVoiceFloatingView.show();
        mVoiceFloatingView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.showShort("点击了");
                Intent mainIntent = new Intent(CameraService.this, MainActivity.class);
                mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Intent cameraIntent = new Intent(CameraService.this, CameraActivity.class);
                Intent[] intents = {mainIntent, cameraIntent};
                startActivities(intents);
                mVoiceFloatingView.dismiss();
            }
        });
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mVoiceFloatingView.dismiss();
        mVoiceFloatingView = null;
        isStart = false;
        unregisterReceiver(new MyBroadcastReceiver());
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
